package com.biyao.service.model.product.imp;

import com.biyao.schedulingentity.base.TCraneEntity;
import com.biyao.schedulingentity.base.TGrinderEntity;
import com.biyao.schedulingentity.base.TRollerRacksEntity;
import com.biyao.schedulingentity.product.*;
import com.biyao.service.model.base.TCraneService;
import com.biyao.service.model.base.TGrinderMessage;
import com.biyao.service.model.base.TRollerRacksMessage;
import com.biyao.service.model.product.ProductionPlanIMessage;
import com.biyao.service.model.product.ProductionPlanMessage;
import com.biyao.service.model.product.ProductionPlanTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class ProductionPlanTemplateService implements ProductionPlanTemplateMessage {

    @Autowired
    private ProductionPlanMessage productionPlanMessage;

    @Autowired
    private ProductionPlanIMessage productionPlanIMessage;

    @Autowired
    private TCraneService tCraneService;
    @Autowired
    private TGrinderMessage tGrinderMessage;
    @Autowired
    private TRollerRacksMessage tRollerRacksMessage;

    @Override
    public ProductionPlanTemplateCopy selectProductionPlanTemplate(Integer id) {
        TProductionPlanEntity tProductionPlanEntity= productionPlanMessage.selectProductionPalnById(id);
        List<TProductionPlanIEntity> tProductionPlanIEntityList=productionPlanIMessage.selectProductionByPlanId(id);
        List<TProductionPlanICopy> list=new ArrayList<>();
        for(TProductionPlanIEntity tProductionPlanIEntity:tProductionPlanIEntityList){
            TProductionPlanICopy tProductionPlanICopy=new TProductionPlanICopy();
            BeanUtils.copyProperties(tProductionPlanIEntity,tProductionPlanICopy);
            if(tProductionPlanIEntity.getParam1()!=null){
                if(tProductionPlanIEntity.getTaskCode().equals("001")){
                  //天车
                 TCraneEntity tCraneEntity= tCraneService.selectById(tProductionPlanIEntity.getParam1());
                 if(tCraneEntity!=null){
                     tProductionPlanICopy.setParam1Name(tCraneEntity.getCode());
                 }
                }else if(tProductionPlanIEntity.getTaskCode().equals("002")){
                    //磨床
                    TGrinderEntity tGrinderEntity=tGrinderMessage.selectEntryById(Integer.parseInt(tProductionPlanIEntity.getParam1()));
                    if(tGrinderEntity!=null){
                        tProductionPlanICopy.setParam1Name(tGrinderEntity.getCode());
                    }
                }

            }
            //辊架
            if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam2())){
               TRollerRacksEntity tRollerRacksEntity= tRollerRacksMessage.selectTRollerEntityById(Integer.parseInt(tProductionPlanIEntity.getParam2()));
                tProductionPlanICopy.setParam2Name(tRollerRacksEntity.getCode());
            }
            if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam3())){
                TRollerRacksEntity tRollerRacksEntity= tRollerRacksMessage.selectTRollerEntityById(Integer.parseInt(tProductionPlanIEntity.getParam3()));
                tProductionPlanICopy.setParam3Name(tRollerRacksEntity.getCode());
            }
            if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam4())){
                if(tProductionPlanIEntity.getParam4().equals("1")){
                    tProductionPlanICopy.setParam4Name("上");
                }else if(tProductionPlanIEntity.getParam4().equals("2")){
                    tProductionPlanICopy.setParam4Name("下");
                }else if(tProductionPlanIEntity.getParam4().equals("3")){
                    tProductionPlanICopy.setParam4Name("一对");
                }
            }
            if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam5())){
                if(tProductionPlanIEntity.getParam5().equals("1")){
                    tProductionPlanICopy.setParam5Name("上");
                }else if(tProductionPlanIEntity.getParam5().equals("2")){
                    tProductionPlanICopy.setParam5Name("下");
                }else if(tProductionPlanIEntity.getParam5().equals("3")){
                    tProductionPlanICopy.setParam5Name("一对");
                }
            }
            if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam7())){
                TGrinderEntity tGrinderEntity=tGrinderMessage.selectEntryById(Integer.parseInt(tProductionPlanIEntity.getParam7()));
                if(tGrinderEntity!=null){
                    tProductionPlanICopy.setParam2Name(tGrinderEntity.getCode());
                }
                tProductionPlanICopy.setParam2("+"+tProductionPlanIEntity.getParam7());
            }
            if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam8())){
                TGrinderEntity tGrinderEntity=tGrinderMessage.selectEntryById(Integer.parseInt(tProductionPlanIEntity.getParam8()));
                if(tGrinderEntity!=null){
                    tProductionPlanICopy.setParam3Name(tGrinderEntity.getCode());
                }
                tProductionPlanICopy.setParam3("+"+tProductionPlanIEntity.getParam8());
            }

            if(!StringUtils.isEmpty(tProductionPlanIEntity.getTaskCode())&&"001".equals(tProductionPlanIEntity.getTaskCode())){
                tProductionPlanICopy.setTaskName("移动轧辊");
            }else if(!StringUtils.isEmpty(tProductionPlanIEntity.getTaskCode())&&"002".equals(tProductionPlanIEntity.getTaskCode())){
                tProductionPlanICopy.setTaskName("磨辊");
            }

            list.add(tProductionPlanICopy);

        }
        ProductionPlanTemplateCopy productionPlanTemplateEntity=new ProductionPlanTemplateCopy();
        productionPlanTemplateEntity.setList(list);
        productionPlanTemplateEntity.setTProductionPlanEntity(tProductionPlanEntity);
        return productionPlanTemplateEntity;
    }

    @Override
    public Integer addProductionPlanTemplate(ProductionPlanTemplateEntity productionPlanTemplateEntity) {
        Integer id=productionPlanMessage.addProduction(productionPlanTemplateEntity.getTProductionPlanEntity());
        List<TProductionPlanIEntity> list=productionPlanTemplateEntity.getList();
        addProductionPlan(list,id);
        return id;

    }

    @Override
    public void updateProductPlanTemplate(ProductionPlanTemplateEntity productionPlanTemplateEntity) {
        Integer productionId=productionPlanTemplateEntity.getTProductionPlanEntity().getId();
        productionPlanMessage.updateProduction(productionPlanTemplateEntity.getTProductionPlanEntity());
        productionPlanIMessage.deleteByProductId(productionId);
        List<TProductionPlanIEntity> list=productionPlanTemplateEntity.getList();
        addProductionPlan(list,productionId);
    }

    public void addProductionPlan(List<TProductionPlanIEntity> list,Integer productionId){
        List<TProductionPlanIEntity> listT1=new ArrayList<>();
        List<TProductionPlanIEntity> listT2=new ArrayList<>();
        for(TProductionPlanIEntity tProductionPlanIEntity:list){
            if ("1000".equals(tProductionPlanIEntity.getParam1()) || "100".equals(tProductionPlanIEntity.getParam1()) || "101".equals(tProductionPlanIEntity.getParam1())) {
                listT1.add(tProductionPlanIEntity);
            }
        }

        for(TProductionPlanIEntity tProductionPlanIEntity:list){
            if ("1001".equals(tProductionPlanIEntity.getParam1()) || "102".equals(tProductionPlanIEntity.getParam1()) || "103".equals(tProductionPlanIEntity.getParam1())) {
                listT2.add(tProductionPlanIEntity);
            }
        }
        if(listT1.size()>0){
           Collections.sort(listT1, new Comparator<TProductionPlanIEntity>() {
               @Override
               public int compare(TProductionPlanIEntity o1, TProductionPlanIEntity o2) {
                   return o1.getSequence().compareTo(o2.getSequence());
               }
           });
            for(int i=0; i<listT1.size();i++){
            if(i==0){
                listT1.get(i).setExecutionPosition(true);
            }
                listT1.get(i).setProductionPlanId(productionId);
            productionPlanIMessage.addProductionI(listT1.get(i));
             }
        }
        if(listT2.size()>0){
            Collections.sort(listT2, new Comparator<TProductionPlanIEntity>() {
                @Override
                public int compare(TProductionPlanIEntity o1, TProductionPlanIEntity o2) {
                    return o1.getSequence().compareTo(o2.getSequence());
                }
            });
            for(int i=0; i<listT2.size();i++){
                if(i==0){
                    listT2.get(i).setExecutionPosition(true);
                }
                listT2.get(i).setProductionPlanId(productionId);
                productionPlanIMessage.addProductionI(listT2.get(i));
            }
        }


    }
    @Override
    public void deleteProductPlanTemplate(Integer id) {
        productionPlanMessage.deleteProductionPlanById(id);
        productionPlanIMessage.deleteByProductId(id);
    }
}
